package com.hunk.core.auth.dto;

import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.hunk.core.auth.entity.Role;
import com.hunk.core.auth.entity.User;
import com.hunk.core.auth.entity.UserRole;
import com.hunk.utils.JacksonUtil;
import com.hunk.utils.io.Print;

@JsonAutoDetect
public class UserRoleDTO {
	private User user;
	private Set<RoleDTO> roles = new LinkedHashSet<RoleDTO>();
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Set<RoleDTO> getRoles() {
		return roles;
	}
	public void setRoles(Set<RoleDTO> roles) {
		this.roles = roles;
	}
	
	/**
	 * @author HunkDeng
	 * @return
	 */
	public Set<UserRole> gen() {
		Set<UserRole> set = new HashSet<UserRole>();
		Iterator<RoleDTO> ite = roles.iterator();
		while (ite.hasNext()) {
			UserRole ur = new UserRole();
			Role role = new Role();
			role.setId(ite.next().data().getId());
			ur.setRole(role);
			ur.setUser(user);
			set.add(ur);
		}
		return set;
	}
	
	public static void main(String[] args) {
		String str = "{\"user\":{\"id\":\"fbc6c0c6-bc0d-4152-b3dd-a37c4fa9bc86\"},\"roles\":[{\"id\":\"003\",\"pid\":\"001\",\"text\":\"subadmin\",\"expanded\":true,\"data\":{\"id\":\"003\",\"roleName\":\"subadmin\",\"description\":\"subadmin\",\"isSystem\":true,\"parentRole\":{\"id\":\"001\",\"roleName\":\"Administrator1\",\"description\":\"Administrator\",\"isSystem\":true,\"parentRole\":{\"id\":\"root\",\"roleName\":\"RoleRoot\",\"description\":\"tree_root\",\"isSystem\":true,\"parentRole\":null}}},\"nid\":\"mytree1_3\"},{\"id\":\"002\",\"pid\":\"root\",\"text\":\"normal\",\"expanded\":true,\"data\":{\"id\":\"002\",\"roleName\":\"normal\",\"description\":\"normal\",\"isSystem\":false,\"parentRole\":{\"id\":\"root\",\"roleName\":\"RoleRoot\",\"description\":\"tree_root\",\"isSystem\":true,\"parentRole\":null}},\"nid\":\"mytree1_5\"}]}";
		UserRoleDTO dto = JacksonUtil.getBeanFromJson(str, UserRoleDTO.class);
		Print.println(dto.gen());
	}
}
